package com.app.business;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import jxl.Sheet;
import com.utils.UtilsExcel;
import com.utils.UtilsFile;
import com.utils.UtilsTaobao;
import com.utils.Constants;
import com.utils.UtilsLog;

public class ReadProSheet {
	private File logFile;
	
	public Map<String, String> getItemsMap(Sheet sheet, File logFile){
		this.logFile = logFile;
		Map<String, String> userPro = new HashMap<String, String>();
		List<String> list = UtilsExcel.readSheet(sheet);
		
		for (String data : list) {
			userPro.putAll(convertFiled(data));
		}
		
		return userPro;
	}

	
	
	public Map<String, String> convertFiled(String data) {
		String[] splitData = data.split(Constants.SPLIT_SIGN);
		int columns_nums = UtilsExcel.judgeColumnNum(splitData);
		int data_nums = columns_nums + 1;
		String[] columns = splitData[columns_nums].split(Constants.SPLIT_SIGN_CELL);
		
		for (int i = 0; i < columns.length; i++) {
			columns[i] = (String) (Constants.FIELD_MAPPING_CUSTOM.containsKey(columns[i]) == true ? Constants.FIELD_MAPPING_CUSTOM.get(columns[i]) : columns[i]);
			columns[i] = (String) (Constants.FIELD_MAPPING_TAOBAO.containsKey(columns[i]) == true ? Constants.FIELD_MAPPING_TAOBAO.get(columns[i]) : columns[i]);
		}
		
		String cid = "";
		String pinhao = "";
		String skuStr = "";
		String[] rowDataArray = null;
		String rowCellValue = "";
		StringBuffer sb = new StringBuffer();
		Map<String, String> map2 = new HashMap<String, String>();
		Map<String, String> userPro = new HashMap<String, String>();
		for (int i = data_nums; i < splitData.length; i++) {
			rowDataArray = splitData[i].split(Constants.SPLIT_SIGN_CELL);
			for (int j = 0; j < rowDataArray.length; j++) {
				rowCellValue = rowDataArray[j];
				/** 
				 * 判断此单元格是否是空格
				 */
				if(rowCellValue.trim().length()<1) continue;
				
				/** 单元格值映射。如果为基本属性则不做映射，非基本属性才做映射*/
				if(!Constants.FIELD_NOT_MAPPING.containsKey(columns[j])){
					rowCellValue = (String) (Constants.FIELD_MAPPING_CUSTOM.containsKey(rowCellValue) == true ? Constants.FIELD_MAPPING_CUSTOM.get(rowCellValue) : rowCellValue);
				}
				
				/** 如果此列为基本属性，则拼装基本属性的xml*/
				if(Constants.FIELD_MAPPING_TAOBAO.containsValue(columns[j])){
					if(columns[j].equals("cid")){
						cid = UtilsTaobao.queryItemCatsNodeVal("30", rowCellValue);
						if(StringUtils.isBlank(cid)){
							UtilsFile.LogToFile(logFile, "错误: [" + rowCellValue + "]此商品的类别在淘宝分类中无法找到! 无法添加到淘宝!" + splitData[i], true);
							break;
						}
						sb.append("<cid>" + cid + "</cid>");
					}else if(columns[j].equals("outer_id")){
						try {
							pinhao = rowCellValue;
							String pinhaoCode = UtilsTaobao.queryItemPropsNodeVal(cid, "货号");
							sb.append("<input_pids>" + pinhaoCode +"<input_pids/>");
							sb.append("<input_str>" + rowCellValue +"<input_str/>");
							map2.put(pinhaoCode, rowCellValue);
						} catch (IOException e) {
							UtilsLog.LOG.error("find 'outer_id' excption! excel place: [" + i + ", " + j +"]" +", value:["+rowCellValue+"]");
						}
					}else if(columns[j].equals("title")){
						sb.append("<title>" + rowCellValue +"</title>");
					}
				}else{
					/** 如果此列为非基本属性(商品属性)，则拼装非基本属性的xml*/
					if(!StringUtils.isBlank(cid) && !Constants.FIELD_IGNORED.containsKey(columns[j])){
						try {
							/** 查询此属性的代码id*/
							String str1 = null;
							if(columns[j].equals("颜色")){
								sb.append("<color>" + rowCellValue +"</color>");
							}else{
								str1 = UtilsTaobao.queryItemProps(cid, columns[j], rowCellValue);
								if(str1 != null){
										String key = UtilsTaobao.queryItemPropsNodeVal(cid, columns[j]);
										skuStr = skuStr + UtilsTaobao.queryItemProps(cid, columns[j], rowCellValue);
										map2.put(key, rowCellValue);
								}else{
									UtilsFile.LogToFile(logFile, "警告: [" +pinhao+ "]一个属性未找到: " + columns[j] + "： " + rowCellValue, true);
								}
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		
			/**
			 * 判断没有的必须属性，并补齐必须属性
			 */
			if(!StringUtils.isBlank(cid)){
				Map<String, String> map1 = UtilsTaobao.queryItemPropsMust(cid + ".xml");
				String skuStrMustOther = "";
				if(map1 == null){
					UtilsLog.LOG.error(cid + "的属性文件未找到，未校验属性！");
				}else{
					Set<String> entrySet1 = map1.keySet();
					Set<String> entrySet2 = map2.keySet();
					String[] value = null;
					if(!entrySet2.containsAll(entrySet1)){
						for (String key : entrySet1) {
							if(!map2.containsKey(key) && !key.equals(Constants.ITEM_BRAND_KEY)){
								//pid_name$vid_name$vid_value
								value = (map1.get(key)).split("@");
								skuStrMustOther = skuStrMustOther + key + ":" + value[2] + ";";
								UtilsFile.LogToFile(logFile, "警告: [" +pinhao+ "]一个必须属性没有: " + value[0] + ",默认为第一个 " + value[1], true);
							}
						}
					}
				}
				
				sb.append("<props>" + (skuStr + skuStrMustOther + (Constants.ITEM_BRAND_KEY + ":" + Constants.ITEM_BRAND_VALUE + ";")) + "</props>");
				userPro.put(pinhao,sb.toString());
				sb = sb.delete(0, sb.length());
				skuStr = "";
				skuStrMustOther = "";
				cid = "";
				map2.clear();
			}
		}
		
		return userPro;
	}
	
}
